package com.ukao.cashregister.pesenter;

import android.content.Context;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.ChooseTag;
import com.ukao.cashregister.bean.Coupons;
import com.ukao.cashregister.bean.RechargeBean;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.bean.UserInfoBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.ThreadUtils;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.UserInfoView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class UserInfoPesenter extends BasePresenter<UserInfoView> {

    public UserInfoPesenter(UserInfoView view) {
        attachView(view);
    }



    /**
     *用户信息
     */
    public void userinfoData (String useid) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",useid) ;
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userInfoDetail(Constant.createParameter(mHashMap)),
                new ApiCallback<UserInfoBean.UserBean>() {
                    @Override
                    public void onSuccess(UserInfoBean.UserBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.userInfoSucceed(model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());
                        }
                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }

                });
    }

    /**
     *修改姓名
     */
    public void updateName (Context context, String id,final String name  ) {

        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id) ;
        mHashMap.put("name",name);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.updateName(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                       if (model.getHttpCode()!=200){
                         mvpView.updateNameFail(model.getMsg(),name);
                       }
                    }



                    @Override
                    public void onFinish() {

                    }

                });
    }

    /**
     *手机号
     */
    public void updatePhone(Context context,String id,String phone  ) {
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id) ;
        mHashMap.put("phone",phone);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.updatePhone (Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                      //  mvpView.getDataSuccess(model);
                    }



                    @Override
                    public void onFinish() {
                    //    mvpView.hideLoading();
                    }

                });
    }


    /**
     *修改性别
     */
    public void updateSex(Context context,String id,String sex, final String sexname  ) {
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id) ;
        mHashMap.put("sex",sex);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.updateSex (Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()!=200){
                            mvpView.updateSexFail(model.getMsg(),sexname);
                        }
                    }



                    @Override
                    public void onFinish() {

                    }

                });
    }


    /**
     *修改生日
     */
    public void updateBirthday(Context context,String id,String birthday,final  String strbirthday  ) {

        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",id);
        mHashMap.put("birthday",birthday);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.updateBirthday(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()!=200){
                            mvpView.updateBirthdayFail(model.getMsg(),strbirthday);
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *优惠券
     */
    public void couponInfoList (String userId ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("userId",userId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.couponInfoList (Constant.createParameter(mHashMap)),
                new ApiCallback<Coupons>() {
                    @Override
                    public void onSuccess(Coupons model) {
                        if (model.getHttpCode()==200){
                                mvpView.receiveCouponsData(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     *用户已添加的的标签列表
     */
    public void userTaglist (String userId ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("userId",userId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userTaglist(Constant.createParameter(mHashMap)),
                new ApiCallback<ChooseTag>() {
                    @Override
                    public void onSuccess(ChooseTag model) {
                        if (model.getHttpCode()==200){

                            mvpView.loadUseTagData(model.getData().getList());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     所有标签列表
     */
    public void userTagAlllist (  ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.userTagAlllist (Constant.createParameter(mHashMap)),
                new ApiCallback<ChooseTag>() {
                    @Override
                    public void onSuccess(ChooseTag model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadAllTagData(model.getData().getList());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     所有标签列表
     */
    public void updateTag (String userId,String  tags) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("id",userId);
        mHashMap.put("tags",tags);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.updateTag (Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.updateTagSucceed();
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }




    /*查询充值优惠列表*/
    public void queryRechargeList(Context mContext) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.queryRechargeList(Constant.createParameter(mHashMap)),
                new ApiCallback<RechargeBean>() {
                    @Override
                    public void onSuccess(RechargeBean model) {
                        if (model.getHttpCode() == 200) {
                          /*  {"httpCode":400,"msg":"该RFID还未绑定身份","timestamp":1504859442501}*/
                            mvpView.rechargeTableSucceed(model.getData());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();


                    }
                });
    }

    /**
     绑定RFID卡
     */
    public void updateCard (final String userId,final String rfidSecret  ) {
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mvpView.showLoading();
                HashMap     mHashMap=new HashMap<String,String>();
                mHashMap.put("userId",userId);
                mHashMap.put("rfidSecret",rfidSecret);
                mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
                addSubscription(apiStores.updateCard (Constant.createParameter(mHashMap)),
                        new ApiCallback<StringBean>() {
                            @Override
                            public void onSuccess(StringBean model) {
                                if (model.getHttpCode()==200){
                                    mvpView.updateCardSucceed("绑卡成功",model.getData());
                                }else{
                                    mvpView.loadFail(model.getMsg());
                                }

                            }



                            @Override
                            public void onFinish() {
                                mvpView.hideLoading();
                            }

                        });
            }
        });
    }
    /**
     解绑RFID     isShowDialog 是否显示成功对话框
     */
    public void unbundling (String userId   ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("userId",userId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.unbundling (Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.unbindCardSucceed("已解绑");
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }
                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     作废RFID
     */
    public void cancel (String userId ) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("userId",userId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.cancel(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.lossSucceed("已作废");
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *输入卡号验证码绑定用户
     */
    public void codebinding(String userId,final String rfidNo,String checkCode  ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("userId", userId);
        mHashMap.put("rfidNo", rfidNo);
        mHashMap.put("checkCode", checkCode);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.codebinding(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.updateCardSucceed("绑卡成功",rfidNo);
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
}
